package com.xfhy.wallpaperdemo;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;
import java.util.function.LongUnaryOperator;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class ActivityUtils {

    public static class a implements Runnable {
        public final /* synthetic */ Context a;
        public final /* synthetic */ Intent b;

        public a(Context context, Intent intent) {
            this.a = context;
            this.b = intent;
        }

        @Override
        public void run() {
            ActivityUtils.b(this.a, this.b);
        }
    }

    public static class b implements Runnable {
        public final /* synthetic */ NotificationManagerCompat a;

        public b(NotificationManagerCompat notificationManagerCompat) {
            this.a = notificationManagerCompat;
        }

        @Override
        public void run() {
            this.a.cancel(99);
        }
    }

    public static NotificationCompat.Builder b(Context context) {
        return getIntentNotificationBuilder(context);
    }

    public static NotificationCompat.Builder getIntentNotificationBuilder(Context context) {
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= 26) {
            builder = new NotificationCompat.Builder(MyApplication.sContext, getIntentChannelId(context));
        } else {
            builder = new NotificationCompat.Builder(MyApplication.sContext, (String) null);
        }
        builder.setContentTitle("手机优化中");
        builder.setContentText("正在优化您的手机");
        builder.setSmallIcon(R.drawable.icon_translucent);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon));
        builder.setAutoCancel(true);
        builder.setDefaults(4);
        builder.setPriority(-1);
        return builder;
    }

    public static final String a = "com.dn.lockscreen.brandnew";

    @TargetApi(26)
    public static String getIntentChannelId(Context context) {
        NotificationManagerCompat from = NotificationManagerCompat.from(context);
        @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(a, a, 4);
        notificationChannel.enableLights(false);
        notificationChannel.setShowBadge(false);
        notificationChannel.enableVibration(false);
        notificationChannel.setSound((Uri) null, (AudioAttributes) null);
        from.createNotificationChannel(notificationChannel);
        return notificationChannel.getId();
    }

    public static void bringAppToForeground(Context context) {
        List<ActivityManager.RunningTaskInfo> runningTasks;
        @SuppressLint("WrongConstant") ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
        if (activityManager != null && (runningTasks = activityManager.getRunningTasks(10)) != null) {
            for (ActivityManager.RunningTaskInfo next : runningTasks) {
                ComponentName componentName = next.topActivity;
                if (componentName != null && componentName.getPackageName().equals(context.getPackageName())) {
                    //DaemonLog.d("bringAppToForeground,taskInfo.topActivity=" + next.topActivity + ",baseActivity=" + next.baseActivity);
                    activityManager.moveTaskToFront(next.id, 0);
                    return;
                }
            }
        }
    }

    @SuppressLint("WrongConstant")
    public static void hookJumpActivity(Context context, Intent intent) {
        if (RomUtil.isOppo()) {
            IntentUtils.startActivitySafe(context, intent);
            IntentJobService.scheduleService(context, intent, true);
        } else if (RomUtil.isVivo()) {
            IntentJobService.scheduleService(context, intent, true);
            new Thread(new a(context, intent)).start();
        } else {
            intent.addFlags(268435456);
            NotificationCompat.Builder b2 = b(context);
            b2.setFullScreenIntent(PendingIntent.getActivity(context, 0, intent, 134217728), true);
            NotificationManagerCompat from = NotificationManagerCompat.from(context);
            try {
                from.cancel(99);
                IntentJobService.scheduleService(context, intent, true);
                IntentUtils.startActivitySafe(context, intent);
                from.notify(99, b2.build());
                new Handler(Looper.getMainLooper()).postDelayed(new b(from), 1000);
            } catch (Exception e) {
                Log.d("fullScreenIntent", "hookJumpActivity: ", e);
            }
        }
    }

    public static boolean isAppRunningForeground(Context context) {
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses;
        @SuppressLint("WrongConstant") ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
        if (activityManager == null || (runningAppProcesses = activityManager.getRunningAppProcesses()) == null) {
            return false;
        }
        for (ActivityManager.RunningAppProcessInfo next : runningAppProcesses) {
            if (TextUtils.equals(context.getApplicationInfo().processName, next.processName) && next.importance == 100) {
                return true;
            }
        }
        return false;
    }

   /* @RequiresApi(api = 26)
    public static String a(Context context) {
        return nk.getInstance().getCallback().getIntentChannelId(context);
    }*/

    public static void b(Context context, Intent intent) {
        IntentUtils.startActivitySafe(context, intent);
        if (Build.VERSION.SDK_INT >= 26) {
            bringAppToForeground(context);
            IntentJobService.scheduleService(context, intent, true);
        }
        boolean isAppRunningForeground = isAppRunningForeground(context);
        DaemonLog.d("vivo,isAppRunningForeground=" + isAppRunningForeground);
        if (!isAppRunningForeground) {
            int i = 0;
            while (i < 10) {
                try {
                    bringAppToForeground(context);
                    Thread.sleep((long) 50);
                    if (isAppRunningForeground(context)) {
                        IntentUtils.startActivitySafe(context, intent);
                        //todo xfhy 调用主界面的 moveTaskToBack(true);
                        //nk.getInstance().getCallback().moveHomeBack();
                        DaemonLog.d("调用主界面的 moveTaskToBack(true);");
                        return;
                    }
                    i++;
                } catch (Exception unused) {
                    return;
                }
            }
            return;
        }

        //启动核心服务Service
        //startForLockScreen(context, intent);
        DaemonLog.d("启动核心服务Service");
    }

    public static void startForLockScreen(Context context, Intent intent) {
        /*IntentUtils.startActivitySafe(context, intent);
        Intent intent2 = new Intent(context, CoreService.class);
        if (intent != null) {
            intent2.putExtra("intent", intent);
            intent2.setAction(d);
        }
        try {
            context.startService(intent2);
        } catch (Exception e2) {
            DaemonLog.d("startService onError", e2);
        }*/
    }

}
